// 时间戳转换日期 (yyyy-MM-dd HH:mm:ss) 
function formatDateTime(timeValue) {
  var date = null;
  var param = timeValue.toString();
  if (param.length == 10) {
    date = new Date(parseFloat(param) * 1000);
  } else if (param.length == 13) {
    date = new Date(parseFloat(param));
  } else {
    return '';
  }
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  var h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  var minute = date.getMinutes();
  var second = date.getSeconds();
  minute = minute < 10 ? ('0' + minute) : minute;
  second = second < 10 ? ('0' + second) : second;
  return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
};
//判断传入日期是否为昨天 
function isYestday(timeValue) {
  var date = (new Date());
  //当前时间 
  var today = new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();
  //今天凌晨 
  var yestday = new Date(today - 24 * 3600 * 1000).getTime();
  return timeValue < today && yestday <= timeValue;
};
//判断传入日期是否属于今年 
function isYear(timeValue) {
  var takeNewYear = formatDateTime(new Date()).substr(0, 4);
  //当前时间的年份 
  var takeTimeValue = formatDateTime(timeValue).substr(0, 4);
  //传入时间的年份 
  return takeTimeValue == takeNewYear;
}
//60000 1分钟 //3600000 1小时 //86400000 24小时 //对传入时间进行时间转换   传入13位的
function timeChange(timeValue) {
  var param = timeValue.toString();
  if (param.length == 10) {
    timeValue = new Date(parseFloat(param) * 1000);
  } else if (param.length == 13) {
    timeValue = new Date(parseFloat(param));
  }
  timeValue = Date.parse(timeValue);
  var timeNew = Date.parse(new Date());
  //当前时间 
  var timeDiffer = timeNew - timeValue;
  //与当前时间误差 
  var returnTime = '';
  if (timeDiffer <= 60000) {
    //一分钟内 
    returnTime = '刚刚';
  } else if (timeDiffer > 60000 && timeDiffer < 3600000) {
    //1小时内 
    returnTime = Math.floor(timeDiffer / 60000) + '分钟前';
  } else if (timeDiffer >= 3600000 && timeDiffer < 86400000 && isYestday(timeValue) === false) {
    //今日 
    returnTime = formatDateTime(timeValue).substr(11, 5);
  } else if (timeDiffer > 3600000 && isYestday(timeValue) === true) {
    //昨天 
    returnTime = '昨天' + formatDateTime(timeValue).substr(11, 5);
  } else if (timeDiffer > 86400000 && isYestday(timeValue) === false && isYear(timeValue) === true) {
    //今年 
    returnTime = formatDateTime(timeValue).substr(5, 11);
  } else if (timeDiffer > 86400000 && isYestday(timeValue) === false && isYear(timeValue) === false) {
    //不属于今年 
    returnTime = formatDateTime(timeValue).substr(2, 8);
  }
  return returnTime;
}

var faces = function () { //表情库
  var alt = [{ url: 'qqWeiXiao.gif', alt: '[微笑]' }, { url: 'qqCiYa.gif', alt: '[呲牙]' }, { url: 'qqHuaiXiao.gif', alt: '[坏笑]' }, { url: 'qqHuaJi.gif', alt: '[滑稽]' }, { url: 'qqXiaoKu.gif', alt: '[笑哭]' }, { url: 'qqSe.gif', alt: '[色]' }, { url: 'qqKu.gif', alt: '[酷]' }, { url: 'qqKeAi.gif', alt: '[可爱]' }, { url: 'qqHaiXiu.gif', alt: '[害羞]' }, { url: 'qqDaBing.gif', alt: '[大兵]' }, { url: 'qqBuKaiXin.gif', alt: '[不开心]' }, { url: 'qqBaiYan.gif', alt: '[白眼]' }, { url: 'qqBiShi.gif', alt: '[鄙视]' }, { url: 'qqDai.gif', alt: '[呆]' }, { url: 'qqKun.gif', alt: '[困]' }, { url: 'qqFenDou.gif', alt: '[奋斗]' }, { url: 'qqGuZhang.gif', alt: '[鼓掌]' }, { url: 'qqYun.gif', alt: '[晕]' }, { url: 'qqHan.gif', alt: '[汗]' }, { url: 'qqHaoDe.gif', alt: '[好的]' }, { url: 'qqWeiQu.gif', alt: '[委屈]' }, { url: 'qqKeLian.gif', alt: '[可怜]' }, { url: 'qqKouBi.gif', alt: '[抠鼻]' }, { url: 'qqLiuLei.gif', alt: '[流泪]' }, { url: 'qqShengQi.gif', alt: '[生气]' }, { url: 'qqShiMe.gif', alt: '[什么]' }, { url: 'qqShuai.gif', alt: '[衰]' }, { url: 'qqTuXue.gif', alt: '[吐血]' }, { url: 'qqZaiJian.gif', alt: '[再见]' }, { url: 'qqZhenJing.gif', alt: '[震惊]' }, { url: 'qqZhuaKuang.gif', alt: '[抓狂]' }, { url: 'wbAiXin.gif', alt: '[爱心]' }, { url: 'wbBuLiNi.gif', alt: '[不理你]' }, { url: 'wbDaXiao.gif', alt: '[大笑]' }, { url: 'wbDianZan.gif', alt: '[点赞]' }, { url: 'wbGou.gif', alt: '[狗]' }, { url: 'wbGuZhang.gif', alt: '[鼓掌2]' }, { url: 'wbHaiXiu.gif', alt: '[害羞2]' }, { url: 'wbJingDaiLiao.gif', alt: '[惊呆了]' }, { url: 'wbNanGuo.gif', alt: '[难过]' }, { url: 'wbQinQin.gif', alt: '[亲亲]' }, { url: 'wbSe.gif', alt: '[色2]' }, { url: 'wbShengQi.gif', alt: '[生气2]' }, { url: 'wbTaoMei.gif', alt: '[挑眉]' }, { url: 'wbTianPing.gif', alt: '[舔屏]' }, { url: 'wbTouXiao.gif', alt: '[偷笑]' }, { url: 'wbZaiJian.gif', alt: '[再见2]' }], arr = {};
  layui.each(alt, function (index, item) {
    arr[item.alt] = szoa.client.url + '/static/img/emoji/' + item.url;
  });
  return arr;
}();

//格化式处理
function contentFormat(content) {
  //支持的html标签
  var html = function (end) {
    return new RegExp('\\n*\\[' + (end || '') + '(code|pre|div|span|p|table|thead|th|tbody|tr|td|ul|li|ol|li|dl|dt|dd|h2|h3|h4|h5)([\\s\\S]*?)\\]\\n*', 'g');
  };
  content = (content || '').replace(/&(?!#?[a-zA-Z0-9]+;)/g, '&amp;')
    .replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/'/g, '&#39;').replace(/"/g, '&quot;') //XSS
    .replace(/img\[([^\s]+?)\]/g, function (img) {  //转义图片
      return '<img class="szim-photos" src="' + img.replace(/(^img\[)|(\]$)/g, '') + '">';
    })
    .replace(/other\([\s\S]+?\)\[[\s\S]*?\]/g, function (str) { //转义文件
      var href = (str.match(/other\(([\s\S]+?)\)\[/) || [])[1];
      var text = (str.match(/\)\[([\s\S]*?)\]/) || [])[1];
      if (!href) return str;
      return '<a class="szim-file" href="' + href + '" download target="_blank"><i class="layui-icon">&#xe61e;</i><cite>' + (text || href) + '</cite></a>';
    })
    .replace(/file\([\s\S]+?\)\[[\s\S]*?\]/g, function (str) { //转义文件
      var href = (str.match(/file\(([\s\S]+?)\)\[/) || [])[1];
      var text = (str.match(/\)\[([\s\S]*?)\]/) || [])[1];
      if (!href) return str;
      return '<a class="szim-file" href="' + href + '" download target="_blank"><i class="layui-icon">&#xe61e;</i><cite>' + (text || href) + '</cite></a>';
    })
    .replace(/video\[([^\s]+?)\]/g, function (video) {  //转义视频
      return '<video src="' + video.replace(/(^video\[)|(\]$)/g, '') + '" data-src="' + video.replace(/(^video\[)|(\]$)/g, '') + '" class="szim-video" szim-event="playVideo" controls muted loop="loop" autoplay="autoplay"></video>';
      // return '<div class="szim-video" szim-event="playVideo" data-src="' + video.replace(/(^video\[)|(\]$)/g, '') + '"><i class="layui-icon">&#xe652;</i></div>';
    })
    .replace(/face\[([^\s\[\]]+?)\]/g, function (face) {  //转义表情 // mp replacedStr = str.replace(/\[([^(\]|\[)]*)\]/g  --//old /\[([^\s\[\]]+?)\]/g
      var alt = face.replace(/^face/g, '');
      return '<img class="emoji" alt="' + alt + '" title="' + alt + '" src="' + faces[alt] + '">';
    })    
    .replace(/tab\([\s\S]+?\)\[[\s\S]*?\]/g, function (str) { //转义链接内部tab
      var href = (str.match(/tab\(([\s\S]+?)\)\[/) || [])[1];
      var text = (str.match(/\)\[([\s\S]*?)\]/) || [])[1];
      if (!href) return str;
      return '<a href="javascript:;" onclick="openview({\'id\':\'flowopenview\',\'url\':\'' + href + '\',\'icon\':\'fa fa-sitemap\',\'title\':\'流程处理\'})">' + text + '</a>';
    })
    .replace(/gwopen\([\s\S]+?\)\[[\s\S]*?\]/g, function (str) { //转义链接内部gwopen
      var href = (str.match(/gwopen\(([\s\S]+?)\)\[/) || [])[1];
      var text = (str.match(/\)\[([\s\S]*?)\]/) || [])[1];
      if (!href) return str;
      return '<a href="javascript:;" onclick="openview({\'id\':\'gwopenview\',\'url\':\'' + href + '\',\'icon\':\'fa fa-sitemap\',\'title\':\'公文阅读\'})">' + text + '</a>';
    })
    .replace(/span\[([^\s]+?)\]/g, function (span) {  //转义特定span
      return '<span class="load-pages">' + span.replace(/(^span\[)|(\]$)/g, '') + '</span>';
    })
    .replace(/a\([\s\S]+?\)\[[\s\S]*?\]/g, function (str) { //转义链接  
      var href = (str.match(/a\(([\s\S]+?)\)\[/) || [])[1];
      var text = (str.match(/\)\[([\s\S]*?)\]/) || [])[1];
      if (!href) return str;
      return '<a href="' + href + '" target="_blank">' + (text || href) + '</a>';
    }).replace(html(), '\<$1 $2\>').replace(html('/'), '\</$1\>') //转移HTML代码
    .replace(/\n/g, '<br>') //转义换行
  return content;
};


//内容格式化
function contentformat(d) {
  if (d.type == 'gwmsg') {
    $result = '【' + d.modu_name + '】gwopen(' + d.m_url + ')[' + (d.put_name) + (d.put_dept_id_name ? '(' + d.put_dept_id_name + ')' : '');
    if (d.flow_brief) {
      $result += ',摘要:' + d.flow_brief + ']';
    } else if (d.brief) {
      $result += ',' + d.brief + ']';
    } else {
      $result += ']';
    }
    return $result;
  } else {
    $result = '【' + d.modu_name + '】tab(' + d.m_url + ')[' + (d.put_name || d.rece_id_name) + (d.put_dept_id_name ? '(' + d.put_dept_id_name + ')' : '');
    if (d.flow_brief) {
      $result += ',摘要:' + d.flow_brief + ']';
    } else {
      $result += ',单号:' + d.flow_number + ']';
    }
    $result += (d.step_id_status ? ',' + d.step_id_status : '');
    return $result;
  }
}

Array.prototype.remove = function (val) {
  var index = this.indexOf(val);
  if (index > -1) {
    this.splice(index, 1);
  }
};

function statusTd(d) {
  var out = '';
  if (typeof (d) != "undefined") {
    d = parseInt(d);
    switch (d) {
      case 0:
        out = '<span class="layui-badge layui-bg-black">未提交</span>';
        break;
      case 1:
        out = '<span class="layui-badge layui-bg-red">待审</span>';
        break;
      case 2:
        out = '<span class="layui-badge layui-bg-green">通过</span>';
        break;
      case 3:
        out = '<span class="layui-badge layui-bg-black">拒绝</span>';
        break;
      case 4:
        out = '<span class="layui-badge layui-bg-black">退回</span>';
        break;
    }
  }
  return out;
}